<!--
  Copyright 2012 Google Inc.
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<%
  # This form is a bit tricky.  It's very simple for new quizzes, and fairly
  # complex for existing quizzes.
%>

<%= form_for(@quiz) do |f| %>
  <% if @quiz.errors.any? %>
    <div id=error_explanation>
      <h2>
        <%= pluralize(@quiz.errors.count, "error") %>
        prohibited this quiz from being saved:
      </h2>

      <ul>
        <% @quiz.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <fieldset>
    <legend>Basic Settings</legend>

    <div class=field>
      <%= f.label :name %>
      <%= f.text_field :name %>
    </div>

    <div class=field>
      <%= f.label :min_age_years %>
      <%= f.text_field :min_age_years %> (years)
    </div>

    <div class=field>
      <%= f.label :country_alpha2 %>
      <%= f.country_select :country_alpha2 %>
    </div>
    
    <% if @quiz.new_record? %>
      <div class="indented_submit spacious_submit">
        <%= f.submit %>
      </div>
    <% end %>
  </fieldset>

  <% unless @quiz.new_record? %>
    <fieldset>
      <legend>Video</legend>

      <% if @quiz.video_id.blank? %>
        <p>This is no video associated with this quiz yet.</p>
      <% elsif @video_entry.blank? %>
        <p>Could not load video: <%= @quiz.video_id %></p>
      <% else %>
        <p>
          <%= link_to @video_entry['data']['title'],
                      @video_entry['data']['player']['default'],
                      :target => '_top' %>
        </p>
      <% end %>

      <p><%= link_to 'Search for a video', quiz_video_search_path(@quiz) %></p>
    </fieldset>

    <fieldset>
      <legend>Form</legend>

      <p>
        To setup a form for your quiz, go to
        <a href="https://docs.google.com">docs.google.com</a> and create a new
        form.  Once you have created the form, click More Actions &gt; Embed.
        Look at the snippet of code, and find just the formkey.  Enter that
        below.  This process is currently a bit manual, but I'm hoping to
        improve it in the days to come.
      </p>

      <div class=field>
        <%= f.label :form_id %>
        <%= f.text_field :form_id %>
      </div>
    </fieldset>

    <div class=spacious_submit>
      <%= f.submit %>
    </div>
  <% end %>
<% end %>